#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <fstream>

#include "Log.hpp"

const std::string sep = " ";
const std::string mypath = "./recourse/dict.txt";

namespace TransformNS
{
    class Translate
    {
    public:
        Translate(const std::string dirt_path = mypath)
            : _dirt_path(dirt_path)
        {
            LoadDict();
            Prase();
        }

        void LoadDict()
        {
            std::fstream in(_dirt_path);
            std::string line;
            while (std::getline(in, line))
            {
                _line.push_back(line);
            }
            in.close();
            lg.LogMessage(Debug, "Load dict.txt success! path:%s", _dirt_path.c_str());
        }

        //
        void Prase()
        {
            for (auto &line : _line)
            {
                auto pos = line.find(sep);
                if (pos == std::string::npos)
                    continue;
                else
                {
                    std::string word = line.substr(0, pos);
                    std::string exlp = line.substr(pos + sep.size());
                    _dict.insert(std::make_pair(word, exlp));
                }
            }
            lg.LogMessage(Debug, "Prase dirt make success!");
        }

        void debug()
        {
            for (auto &elem : _dict)
            {
                std::cout << elem.first << ":" << elem.second << std::endl;
            }
        }

        std::string Excuse(std::string word)
        {
            auto it = _dict.find(word);
            if (it == _dict.end())
                return "unknown";
            else
                return _dict[word];
        }

    private:
        std::string _dirt_path;
        std::unordered_map<std::string, std::string> _dict;
        std::vector<std::string> _line;
    };

}